<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport"
		content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
	<meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
	<link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>


<body>
	<header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
		<a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
			<span class="aui-iconfont aui-icon-left"></span>
		</a>
		<div class="aui-title" id="title">
			发货检测单
		</div>
	</header>
	<div class="aui-content aui-margin-b-15" style="margin-top:50px;">
		</br>
		<div class="aui-content aui-margin-b-15">
			<ul class="aui-list aui-form-list">
				<li class="aui-list-item" style="background-color: #cccccc">
					<div class="aui-list-item-inner">
						<div class="aui-list-item-label">检测单号</div>
						<div class="aui-list-item-input">
							<input type="text" placeholder="检测单号" id="chkBillCode" value="" disabled />
						</div>
					</div>
				</li>
				<li class="aui-list-item">
					<div class="aui-list-item-inner">
						<div class="aui-list-item-label">发货计划</div>
						<div class="aui-list-item-input">
							<input type="text" placeholder="发货计划" id="invoiceplancode" value=""
								onclick="getIdSelector();" />
						</div>
						<i class="aui-input-addon aui-iconfont aui-icon-right" onclick="goInvoiceplancode()"></i>
					</div>
				</li>
				<li class="aui-list-item">
					<div class="aui-list-item-inner">
						<div class="aui-list-item-label">物料</div>
						<div class="aui-list-item-input">
							<input type="text" placeholder="选择物料" id="mrlCode" value="" onclick="getIdSelector();" />
						</div>
						<i class="aui-input-addon aui-iconfont aui-icon-right" onclick="goSelectMrl()"></i>
					</div>
				</li>
				<li class="aui-list-item">
					<div class="aui-list-item-inner">
						<div class="aui-list-item-label">检测模板</div>
						<div class="aui-list-item-input">
							<input type="text" placeholder="选择检测模板" id="chkTemplate" value=""
								onclick="getIdSelector();" />
						</div>
						<i class="aui-input-addon aui-iconfont aui-icon-right" onclick="goSelectChkTemplate()"></i>
					</div>
				</li>
				<li class="aui-list-item">
					<div class="aui-list-item-inner">
						<div class="aui-list-item-label">检测数量</div>
						<div class="aui-list-item-input">
							<input type="number" placeholder="填写数量" id="chkNum" value="" />
						</div>
					</div>
				</li>
			</ul>
		</div>

		<div class="aui-content-padded">
			<p>
			<div class="aui-btn aui-btn-info aui-btn-block" onclick='doInput()'>创建</div>
			</p>
		</div>

		<footer class="aui-bar aui-bar-tab" id="footer">
			<div class="aui-bar-tab-item" tapmode id="scan" onclick="doScan()">
				<i class="aui-iconfont aui-icon-scan aui-text-epblue" style="font-size: 1.8rem"></i>
			</div>
		</footer>

</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript">
	window.onload = function () {
		$("#invoiceplancode").focus();
	}
	var idSelector = "";
	// 获取光标所在组件的id
	function getIdSelector() {
		var e = event || window.event;
		var el = e.srcElement || e.target;
		idSelector = el.id;

		$("#" + idSelector).focus();
		$("#" + idSelector).select();
	}

	apiready = function () {

		initChkBillCode();
		var inplancode = localStorage.getItem('invoiceplancode');
		var mId = localStorage.getItem('qcMrlId');
		var mCode = localStorage.getItem('qcMrlCode');
		var mName = localStorage.getItem('qcMrlName');
		var tempId = localStorage.getItem('qcChkTempId');
		var tempCode = localStorage.getItem('qcChkTempCode');
		var tempName = localStorage.getItem('qcChkTempName');

		if (inplancode != null && inplancode != "") {
			invoiceplancode = inplancode;
			$("#invoiceplancode").attr("value", inplancode);
		}

		if (mId != null && mId != "") {
			mrlId = mId;
			mrlCode = mCode;
			mrlName = mName;
			$("#mrlCode").attr("value", mCode + ":" + mName);
		}
		if (tempId != null && tempId != "") {
			chkTempId = tempId;
			chkTempCode = tempCode;
			chkTempName = tempName;
			$("#chkTemplate").attr("value", tempCode + ":" + tempName);
		}
	};

	var invoiceplancode = "";
	var mrlId = "";
	var mrlCode = "";
	var mrlName = "";
	var chkTempId = "";
	var chkTempCode = "";
	var chkTempName = "";
	var workOrderId = "";
	var workOrderCode = "";
	var opId = "";
	var opCode = "";
	var opName = "";


	function doScan() {
		var moduleScanner = api.require('moduleScanner');
		var params = {
			top: 150, //扫描窗口的上边距
			width: 250, //扫描窗口的宽
			height: 250, //扫描窗口的高
			useColor: false, //是否使用自己设定的颜色(startColor和endColor),
			//如果不使用设定的颜色就使用默认的那张网格图
			startColor: '#0F08C8FF', //渐变起始颜色
			endColor: '#7F08C8FF', //渐变结束颜色
			animTime: 2200, //扫描动画时间,单位为毫秒
		};
		moduleScanner.toScannerActivityForResult(params, function (ret, err) {

			//物料号
			if (idSelector == 'mrlCode') {

				if (invoiceplancode == null || invoiceplancode == "" || invoiceplancode == undefined) {
					alert("请先选择发货计划");
					$("#mrlCode").val("");
				} else {
					//查询物料信息
					getMrl(ret, idSelector);
				}

				//检测模板
			} else if (idSelector == 'chkTemplate') {

				if (mrlCode == null || mrlCode == "" || mrlCode == undefined) {
					alert("请先选择物料");
					$("#chkTemplate").val("");
				} else {
					getTemplate(ret, idSelector);
				}

				//发货单号
			} else if (idSelector == 'invoiceplancode') {
				getInvoiceplan(ret, idSelector);
			}
		});
	}


	//1:物料号-自带扫描回车事件
	$("#invoiceplancode").on('keypress', function (e) { //#keyword为input文本框
		var keycode = e.keyCode;
		var sm = $(this).val();
		if (keycode == '13') {
			getInvoiceplan(sm, "invoiceplancode");
		}
	});

	//2:物料号-自带扫描回车事件
	$("#mrlCode").on('keypress', function (e) { //#keyword为input文本框
		var keycode = e.keyCode;
		var sm = $(this).val();
		if (keycode == '13') {

			if (invoiceplancode == null || invoiceplancode == "" || invoiceplancode == undefined) {
				alert("请先选择发货计划");
				$("#mrlCode").val("");
			} else {
				getMrl(sm, "mrlCode");
			}
		}
	});

	//3:检测模板-自带扫描回车事件
	$("#chkTemplate").on('keypress', function (e) { //#keyword为input文本框
		var keycode = e.keyCode;
		var sm = $(this).val();
		if (keycode == '13') {

			if (mrlCode == null || mrlCode == "" || mrlCode == undefined) {
				alert("请先选择物料号");
				$("#chkTemplate").val("");
			} else {
				getTemplate(sm, "chkTemplate");
			}
		}
	});

	//查询计划订单号
	function getInvoiceplan(code, inputId) {
		var un = localStorage.getItem("un");
		var serverUrl = localStorage.getItem('serverUrl');
		var url = serverUrl + "/app/getInvoiceplan";

		var data = {
			values: {
				code: code,
				un: un
			}
		};

		api.ajax({
			url: url,
			method: 'post',
			dataType: 'json',
			timeout: 300,
			returnAll: false,
			data: data
		}, function (ret, err) {
			if (ret) {
				var data = JSON.stringify(ret);
				var myobj = eval("(" + data + ")");
				if (myobj.code == '0') {
					var mydata = eval(myobj.list);
					if (mydata != null && mydata.length > 0) {
						invoiceplancode = mydata[0].code;

						document.getElementById(inputId).value = invoiceplancode;
						$("#mrlCode").focus();
						var e = "mrlCode";
						idSelector = e;
					} else {
						api.toast({
							msg: '计划单号【"' + code + '"】不存在，请重新扫码',
							duration: 5000
						});
						invoiceplancode = "";
						document.getElementById(inputId).value = ""
						$("#invoiceplancode").focus();
					}

				} else {
					api.toast({
						msg: myobj.message,
						duration: 3000
					});
				}
			}
		});
	}

	function getMrl(code, inputId) {
		var un = localStorage.getItem("un");
		var serverUrl = localStorage.getItem('serverUrl');
		var url = serverUrl + "/app/getMrlInvoiceplanByCode";
		var data = {
			values: {
				invoiceplancode: invoiceplancode,
				mrlCode: code,
				un: un
			}
		};

		api.ajax({
			url: url,
			method: 'post',
			dataType: 'json',
			timeout: 300,
			returnAll: false,
			data: data
		}, function (ret, err) {
			if (ret) {
				var data = JSON.stringify(ret);
				var myobj = eval("(" + data + ")");
				if (myobj.code == '0') {
					var mydata = eval(myobj.list);
					if (mydata.length > 0) {
						mrlId = mydata[0].mrlId;
						mrlCode = mydata[0].mrlCode;
						mrlName = mydata[0].mrlName;

						document.getElementById(inputId).value = mrlCode + ":" + mrlName;
						$("#chkTemplate").focus();
						var e = "chkTemplate";
						idSelector = e;

					} else {
						api.toast({
							msg: '该发货单下没有物料【' + code + '】，请重新扫码',
							duration: 5000
						});
						mrlId = "";
						mrlCode = "";
						mrlName = "";
						document.getElementById(inputId).value = ""
						$("#mrlCode").focus();
					}
				} else {
					api.toast({
						msg: myobj.message,
						duration: 3000
					});
				}
			}
		});
	}

	function getTemplate(code, inputId) {
		var un = localStorage.getItem("un");
		var serverUrl = localStorage.getItem('serverUrl');
		var url = serverUrl + "/app/getChkTempByMrl";
		var data = {
			values: {
				code: code,
				mrlCode: mrlCode,
				un: un
			}
		};

		api.ajax({
			url: url,
			method: 'post',
			dataType: 'json',
			timeout: 300,
			returnAll: false,
			data: data
		}, function (ret, err) {
			if (ret) {
				var data = JSON.stringify(ret);
				var myobj = eval("(" + data + ")");
				if (myobj.code == '0') {
					var mydata = eval(myobj.list);
					if (mydata != null && mydata.length > 0) {
						chkTempId = mydata[0].sysid;
						chkTempCode = mydata[0].code;
						chkTempName = mydata[0].name;

						document.getElementById(inputId).value = mydata[0].code;
					} else {
						api.toast({
							msg: '扫码内容不正确，请重新扫码',
							duration: 5000
						});
						$("#chkTemplate").focus();
					}

				} else {
					api.toast({
						msg: myobj.message,
						duration: 3000
					});
				}
			}
		});

	}

	function initChkBillCode() {
		$("#workCenterCode").focus();
		var un = localStorage.getItem("un");
		var serverUrl = localStorage.getItem('serverUrl');
		var url = serverUrl + "/app/initChkBillCode";

		var data = {
			values: {
				un: un,
			}
		};

		api.ajax({
			url: url,
			method: 'post',
			dataType: 'json',
			timeout: 300,
			returnAll: false,
			data: data
		}, function (ret, err) {
			if (ret) {
				var data = JSON.stringify(ret);
				var myobj = eval("(" + data + ")");

				//返回成功
				if (myobj.code == "0") {
					$("#chkBillCode").attr("value", myobj.chkBillCode);
				} else if (myobj.code == "1") {

					api.toast({
						msg: myobj.message,
						duration: 3000
					});
				}

			}
		});

	}

	//选择发货计划
	function goInvoiceplancode() {
		api.openWin({
			name: 'selInvoiceplanList',
			url: 'selInvoiceplanList.html',
			pageParam: {
				name: "qcChkBillInputFHJ",
			}
		});
	}

	//选择发货计划回调函数
	function setInvoiceplan(code) {
		invoiceplancode = code;
		document.getElementById('invoiceplancode').value = code
	}

	//选择物料
	function goSelectMrl() {
		if (invoiceplancode == null) {
			alert("请先选择发货计划");
		} else {
			api.openWin({
				name: 'selMrlListByInvoiceplan',
				url: 'selMrlListByInvoiceplan.html',
				pageParam: {
					name: "qcChkBillInputFHJ",
					invoiceplancode: invoiceplancode,
				}
			});
		}
	}

	//选择物料回调函数
	function setMrl(id, code, name) {
		mrlId = id;
		mrlCode = code;
		mrlName = name;
		document.getElementById('mrlCode').value = code + ":" + name
	}

	//选择检测模板回调函数
	function setChkTemplate(id, code, name) {
		chkTempId = id;
		chkTempCode = code;
		chkTempName = name;
		$("#chkTemplate").attr("value", code + ":" + name);
	}

	//选择检测模板
	function goSelectChkTemplate() {
		if (mrlId == '') {
			alert("请先选择物料信息");
		} else {
			api.openWin({
				name: 'selChkTemplateList',
				url: 'selChkTemplateList.html',
				pageParam: {
					name: "qcChkBillInputFHJ",
					mrlCode: mrlCode,
				}
			});
		}

	}

	function doInput() {
		var un = localStorage.getItem('un');
		var serverUrl = localStorage.getItem('serverUrl');
		var url = serverUrl + "/app/createUqcmChkBillOfDefOpChkTemp";

		if (invoiceplancode == "") {
			api.alert({
				title: '提示',
				msg: '请选择发货单',
			}, function (ret, err) {

			});
			return;
		}

		if (mrlId == "") {
			api.alert({
				title: '提示',
				msg: '请选择物料',
			}, function (ret, err) {

			});
			return;
		}

		if (chkTempId == "") {
			api.alert({
				title: '提示',
				msg: '请选择检测模板',
			}, function (ret, err) {

			});
			return;
		}
		var chkNum = $("#chkNum").val();
		if (chkNum == "") {
			api.alert({
				title: '提示',
				msg: '请填写检测数量',
			}, function (ret, err) {

			});
			return;
		}

		//增加chkNum验证，必须填写正整数
		var patten = /^\d+$/;
		if (!patten.test(chkNum)) {
			api.alert({
				title: '提示',
				msg: '检测数量请填写正整数',
			}, function (ret, err) {

			});
			return;
		}

		var chkBillCode = $("#chkBillCode").val();
		if (chkBillCode == "") {
			api.alert({
				title: '提示',
				msg: '未生成检测单号',
			}, function (ret, err) {

			});
			return;
		}
		var data = {
			values: {
				un: un,
				mrlId: mrlId,
				mrlCode: mrlCode,
				mrlName: mrlName,
				chkTempId: chkTempId,
				chkNum: chkNum,
				chkBillCode: chkBillCode,
				remark: invoiceplancode //计划订单号
			}
		};

		api.ajax({
			url: url,
			method: 'post',
			dataType: 'json',
			timeout: 300,
			returnAll: false,
			data: data
		}, function (ret, err) {
			if (ret) {
				var data = JSON.stringify(ret);
				var myobj = eval("(" + data + ")");
				if (myobj.code == '0') {
					//创建单据成功，将物料，检测模板写入缓存
					localStorage.setItem('qcMrlId', mrlId);
					localStorage.setItem('qcMrlCode', mrlCode);
					localStorage.setItem('qcMrlName', mrlName);
					localStorage.setItem('qcChkTempId', chkTempId);
					localStorage.setItem('qcChkTempCode', chkTempCode);
					localStorage.setItem('qcChkTempName', chkTempName);
					localStorage.setItem('invoiceplancode', invoiceplancode);

					var billId = myobj.billId;

					// api.closeWin();
					api.openWin({
						name: 'qcChkBillDetailInputFHJ',
						url: 'qcChkBillDetailInputFHJ.html',
						pageParam: {
							name: "qcChkBillInputFHJ",
							billId: billId,
							billCode: chkBillCode
						}
					});
				} else {
					api.toast({
						msg: myobj.message,
						duration: 3000
					});
				}
			}
		});
	}

	function closeWin() {
		api.closeWin();
	}
</script>

</html>